const user_collection = db.collection("user");
var md5 = require("md5");

(function() {
    // 构造函数
    var _con = function(info) {
        // console.log(info);
        var self = this;
        // 类别
        if (info) {
            self.type = "user";
            // 自有id,u_+2位随机数+当前时间的毫秒数转16进制 
            self.id = "u_" + Math.random().toString(16).substr(2, 4) + (new Date()).getTime().toString(16);
            // 积分
            self.integral = 0;
            // 论坛昵称
            self.nickname = info.nick_name || info.name || info.nickname || "user_" + Math.random().toString(16).substr(2, 8);
            // 论坛头像
            self.head_img = info.head_img;
            // 论坛等级
            self.level = info.level;
            // 手机号
            self.mobile_phone = info.mobile_phone;
            // 邮箱
            self.email = info.email;
            // 商城用户_id
            self.shop_id = "";
            // 话题数
            self.posttotal = info.posttotal || 0;
            // 回复数
            self.replytotal = info.replytotal || 0;
        };
    };
    module.exports = _con;
    // 由商城已有用户导入
    _con.prototype.saveFromShop = co.wrap(function*(info) {
        printData("正在创建论坛用户", info);
        // 创建时间
        info.createtime = (new Date()).getTime();
        // 自有id,u_+2位随机数+当前时间的毫秒数转16进制 
        info.id = "u_" + Math.random().toString(16).substr(2, 4) + (new Date()).getTime().toString(16);
        return yield mongo.insertOne(user_collection, info);
    });
    // 用户登陆
    _con.prototype.login = co.wrap(function*(info) {
        printData("用户登录：", info);
        var self = this;
        // 跨域登陆，使用旧版本接口
        res = yield fetch("http://api.dotnar.com/user/login", {
            method: "POST",
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: jsonTOurlencoded({
                name: info.username,
                password: info.password
            })
        });
        var doc = yield res.json();
        var _u = JSON.parse(doc.toString || "{}");
        if (_u.info) {
            var _uu = yield mongo.findOne(user_collection, {
                query: {
                    shop_id: _u._id
                }
            });
            // 如果数据库中没有关联的账户存在，则插入一条新数据
            _uu ? true : _uu = yield self.saveFromShop({
                shop_id: _u._id
            });
            printSuccess(_u.info.name, "登陆成功");
            // 更新登陆时间
            var _time = (new Date()).getTime();
            (_time - (_uu.newlogintime || 0)) > 900000 ? _uu = yield mongo.findOneAndUpdate(user_collection, {
                query: {
                    shop_id: _u._id
                },
                data: {
                    // 最新一次登陆时间
                    newlogintime: _time,
                    // 上次登陆时间
                    logintime: _uu.newlogintime || _time
                }
            }) : printError("用户登录时间间隔较短,不更新登陆时间");
            _uu.value ? _uu.value.info = _u.info : _uu.info = _u.info;
            return _uu.value || _uu;
        };
        return doc;
    });

    // 修改用户信息
    _con.prototype.update = co.wrap(function*(user, info) {
        var doc = yield mongo.findOneAndUpdateByObjectId(user_collection, {
            _id: user._id,
            data: info
        });
        doc = doc.value;
        doc.info = user.info;
        return doc;
    });

    // 获取BBS用户信息
    _con.prototype.info = co.wrap(function*(_id) {
        printData("正在获取用户信息：", _id);
        var doc = yield mongo.findOneByObjectId(user_collection, _id);
        return doc;
    });

    // 插入一条回复的时候，回复数+1、积分+3，删除时-1、积分-3
    _con.prototype.upgradereply = co.wrap(function*(_id, isadd) {
        printData("更新用户回复信息：", _id, isadd);
        var doc = yield mongo.findOneByObjectId(user_collection, _id);
        var integral = parseInt(doc.integral || 0);
        var replytotal = parseInt(doc.replytotal || 0);
        replytotal = isadd ? replytotal + 1 : (replytotal > 0 ? replytotal - 1 : 0);
        integral = isadd ? integral + 3 : (integral > 3 ? integral - 3 : 0);
        doc = yield mongo.findOneAndUpdateByObjectId(user_collection, {
            _id: _id,
            data: {
                replytotal: replytotal,
                integral: integral
            }
        });
        return doc.value;
    });
}());
